package com.zhuiyun.project.api.music.service.impl;

import cn.hutool.core.io.FileUtil;
import com.zhuiyun.project.api.music.entity.SysMusic;
import com.zhuiyun.project.api.music.mapper.MusicMapper;
import com.zhuiyun.project.api.music.model.MusicModel;
import com.zhuiyun.project.api.music.service.MusicService;
import com.zhuiyun.project.api.system.user.entity.SysUser;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.*;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.security.auth.Subject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName MusicServiceImpl
 * @Description TODO
 * @Author XMY
 * @Date 2023/12/6 15:42
 **/
@Service
public class MusicServiceImpl implements MusicService {
    @Autowired
    private MusicMapper musicMapper;
    /**
    * @Author XMY
    * @Description 音频上传
    * @Date 2023/12/6 16:34
    * @Return
    * @param file
    */
    @Override
    public CommonResult audioUpload(MultipartFile file, String username) {
        String originalFilename = file.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);
        long size = file.getSize();
        String uploadFiles =  UploadFileUtils.uploadFile(file);
        Integer uploadFileId = Integer.parseInt(uploadFiles);
        String url = "http://101.43.223.224:8765/image/getVideoById?id=" + uploadFiles;
        SysMusic sysMusic = new SysMusic();
        sysMusic.setMusicName(originalFilename);
        sysMusic.setMusicType(type);
        sysMusic.setMusicSize(size/1024);
        sysMusic.setMusicUrl(url);
        sysMusic.setPicId(uploadFileId);
        sysMusic.setCreateTime(new Date());
        sysMusic.setCreateBy(username);
        sysMusic.setIsDel(0);
        musicMapper.insert(sysMusic);
        return CommonResult.ok();
    }

    /**
    * @Author XMY
    * @Description 音频分页查询
    * @Date 2023/12/6 16:35
    * @Return
    * @param sysMusic
    * @param pageRequest
    */
    @Override
    public CommonResult getMusicList(SysMusic sysMusic, PageRequest pageRequest){
        Example example = new Example(SysMusic.class);
        Object beginTime = sysMusic.getParams().get("beginTime");
        Object endTime = sysMusic.getParams().get("endTime");
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (beginTime != null && endTime != null && StringUtils.isNotBlank(beginTime.toString()) && StringUtils.isNotBlank(endTime.toString())) {
                Date beginTimeDate = dateFormat.parse(beginTime.toString());
                Date endTimeDate = dateFormat.parse(endTime.toString());
                if (beginTimeDate != null && endTimeDate != null) {
//                    example.createCriteria().andBetween("createTime", beginTimeDate, endTimeDate);
                    example.createCriteria().andGreaterThanOrEqualTo("createTime", beginTimeDate).andLessThanOrEqualTo("createTime", endTimeDate);

                }
            }
            if (ObjectUtils.isNotEmpty(sysMusic.getMusicName())) {
                example.createCriteria().andLike("musicName", "%" + sysMusic.getMusicName() + "%");
            }
            if (ObjectUtils.isNotEmpty(sysMusic.getMusicSize())) {
                example.createCriteria().andEqualTo("musicSize", sysMusic.getMusicSize());
            }
            example.createCriteria().andLike("isDel", "0");
            // 设置分页
            MybatisPageHelper.startPage(pageRequest);
            PageResult pageResult = MybatisPageHelper.getPageResult(musicMapper.selectByExample(example));
            return CommonResult.ok(pageResult);
        } catch (ParseException e) {
            // 处理日期转换异常
        }
        return null;
    }
    /**
     * @Author XMY
     * @Description 修改音频信息
     * @Date 2023/12/7 11:27
     * @Return
     * @param musicModel
     */
    @Override
    public CommonResult updateMusic(MusicModel musicModel)throws CommonException {
        // 验证参数的规范性
        if (ValidatorUtil.ValidationObject(musicModel)) {
            SysMusic sysMusic = new SysMusic();
            BeanUtils.copy(musicModel, sysMusic);
            sysMusic.setUpdateTime(new Date());
            int i = musicMapper.updateByPrimaryKeySelective(sysMusic);
            if(i > 0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
    /**
    * @Author 删除/批量删除
    * @Description
    * @Date 2023/12/7 16:25
    * @Return
    * @param picId
    */
    @Override
    public CommonResult delMusicByIds(Integer[] picId) throws CommonException{
        int i = 0;
        boolean f = false;
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(picId)){
            for (Integer c : picId) {
                i = musicMapper.delMusicById(c);
                f = UploadFileUtils.deleteFileById(c);
            }
            if(i>0 && f){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }
}
